﻿@page "/Downloads"

@using BootstrapBlazor.Components

Files:

<pre>
@{
    var files = Directory.GetFiles(UploadPath);
    int i = 0;
    foreach (var item in files)
    {
        i++;
        <button class="btn btn-link" @onclick="(()=>DownloadBlob.DownloadFileFromStream(item,isAndroid:isAndroid))">@($"{i}.{Path.GetFileName(item)}")</button><br />
        }
    }
</pre>

<p>@Message</p>

<DownloadBlob OnError="@OnError" @ref="DownloadBlob" />

<button class="btn btn-primary" @onclick="(async()=>await DownloadTest())">Download File From Stream</button>
<button class="btn btn-secondary" @onclick="StateHasChanged">Reload</button>
<br />


@code{

    protected string UploadPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "uploads");

    string? Message { get; set; }

    [System.Diagnostics.CodeAnalysis.NotNull]
    DownloadBlob? DownloadBlob { get; set; }

#if ANDROID
    bool isAndroid = true;
#else
    bool isAndroid = false;
#endif


    private Task OnError(string message)
    {
        this.Message = message;
        StateHasChanged();
        return Task.CompletedTask;
    }

    private async Task DownloadTest()
    {
        //生成随机文本内存流
        var stream = new MemoryStream(System.Text.Encoding.Default.GetBytes($"Log from blazor {DateTime.Now:F}"));
        var filename= $"test_{Guid.NewGuid()}.txt";
        this.Message = await DownloadBlob.DownloadFileFromStream(filename, stream, isAndroid);
        _ = Task.Run (async() =>
        {
            await Task.Delay(500);
            await InvokeAsync(StateHasChanged);
        });
    }

}